<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:p="http://primefaces.org/ui"
	xmlns:f="http://java.sun.com/jsf/core"
	template="/WEB-INF/template.xhtml">
	<ui:define name="title">Application: #{hsmProxyApplication.selectedApplication.name}</ui:define>
	<ui:define name="body">
		<h:outputScript library="js" name="application.js" target="head" />
		<h:form id="mainForm">
			<p:dataTable id="table" var="row" value="#{hsmProxyApplication.keys}"
				paginator="true" rows="10">
				<f:facet name="header">
					Application keys
				</f:facet>
				<p:column>
					<f:facet name="header">Key</f:facet>
					<h:outputText value="#{row.name}" />
				</p:column>
				<p:column>
					<f:facet name="header">Actions</f:facet>
					<p:commandLink id="removeButton" styleClass="ui-icon ui-icon-trash"
						style="display:inline-block;"
						actionListener="#{hsmProxyApplication.selectApplicationKey}"
						oncomplete="removeDialog.show();" update=":messages,:removeForm">
						<f:attribute name="selectedApplicationKey" value="#{row}" />
					</p:commandLink>
					<p:tooltip for="removeButton" value="Remove key..." />

					<p:commandLink id="editButton" styleClass="ui-icon ui-icon-wrench"
						style="display:inline-block;"
						actionListener="#{hsmProxyApplication.selectApplicationKey}"
						oncomplete="editDialog.show();" update=":messages,:editForm">
						<f:attribute name="selectedApplicationKey" value="#{row}" />
					</p:commandLink>
					<p:tooltip for="editButton" value="Configure key..." />
				</p:column>
				<f:facet name="footer">
					<h:panelGroup style="display:block; text-align: left;">
						<p:commandLink id="addButton" onclick="addDialog.show();"
							styleClass="ui-icon ui-icon-circle-plus"
							style="display:inline-block;" />
						<p:tooltip for="addButton" value="Add key..." />
					</h:panelGroup>
				</f:facet>
			</p:dataTable>

			<br />

			<p:dataTable id="credentialTable" var="row"
				value="#{hsmProxyApplication.credentials}" paginator="true"
				rows="10">
				<f:facet name="header">
					Application credentials
				</f:facet>
				<p:column>
					<f:facet name="header">Hash</f:facet>
					<h:outputText value="#{row.hash}" />
				</p:column>
				<p:column>
					<f:facet name="header">Actions</f:facet>
					<p:commandLink id="removeCredentialButton"
						styleClass="ui-icon ui-icon-trash" style="display:inline-block;"
						actionListener="#{hsmProxyApplication.selectCredential}"
						oncomplete="removeCredentialDialog.show();"
						update=":messages,:removeCredentialForm">
						<f:attribute name="selectedCredential" value="#{row}" />
					</p:commandLink>
					<p:tooltip for="removeCredentialButton"
						value="Remove credential..." />
				</p:column>
				<f:facet name="footer">
					<h:panelGroup style="display:block; text-align: left;">
						<p:commandLink id="addCredentialButton"
							onclick="addCredentialDialog.show();"
							styleClass="ui-icon ui-icon-circle-plus"
							style="display:inline-block;" />
						<p:tooltip for="addCredentialButton" value="Add credential..." />
					</h:panelGroup>
				</f:facet>
			</p:dataTable>
		</h:form>

		<h:form>
			<p:commandButton value="Back" action="#{hsmProxyApplication.back}"
				ajax="false" />
		</h:form>

		<p:dialog widgetVar="addDialog" header="Add key" modal="true">
			<h:form id="addForm">
				<h:panelGrid columns="3">
					<h:outputText value="Name:" />
					<h:inputText id="name" value="#{hsmProxyApplication.keyName}"
						required="true" />
					<p:message for="name" />
				</h:panelGrid>
				<p:commandButton value="Add"
					actionListener="#{hsmProxyApplication.addKey}"
					oncomplete="handleAddApplicationKeyRequest(xhr, status, args)"
					update="addForm,:messages,:mainForm" />
			</h:form>
		</p:dialog>

		<p:confirmDialog header="Remove Key"
			message="Are you sure to remove the key?" severity="alert"
			widgetVar="removeDialog">
			<h:form id="removeForm">
				<p:commandButton id="removeButton" value="Yes"
					actionListener="#{hsmProxyApplication.removeSelectedApplicationKey}"
					oncomplete="removeDialog.hide();" update=":messages,:mainForm" />
				<p:commandButton value="No" onclick="removeDialog.hide();" />
			</h:form>
		</p:confirmDialog>

		<p:dialog widgetVar="editDialog" header="Configure key" modal="true">
			<h:form id="editForm">
				<h:panelGrid columns="2">
					<h:outputText value="Application:" />
					<h:outputText
						value="#{hsmProxyApplication.selectedApplication.name}" />

					<h:outputText value="Key:" />
					<h:outputText
						value="#{hsmProxyApplication.selectedApplicationKey.name}" />

					<h:outputText value="Key Store:" />
					<p:selectOneMenu
						value="#{hsmProxyApplication.selectedApplicationKeyKeyStore}"
						valueChangeListener="#{hsmProxyApplication.selectApplicationKeyKeyStore}">
						<f:selectItems value="#{hsmProxyApplication.keyStores}" />
						<f:ajax render="alias" />
					</p:selectOneMenu>

					<h:outputText value="Key alias:" />
					<p:selectOneMenu id="alias" value="#{hsmProxyApplication.alias}">
						<f:selectItems value="#{hsmProxyApplication.aliases}" />
					</p:selectOneMenu>
				</h:panelGrid>
				<p:commandButton value="Save"
					actionListener="#{hsmProxyApplication.saveSelectedApplicationKey}"
					oncomplete="editDialog.hide();" update=":messages,:mainForm" />
			</h:form>
		</p:dialog>

		<p:dialog widgetVar="addCredentialDialog" header="Add credential"
			modal="true">
			<h:form id="addCredentialForm" enctype="multipart/form-data">
				<h:panelGrid columns="1">
					<p:message for="credentialFileUpload" />
					<p:fileUpload id="credentialFileUpload"
						fileUploadListener="#{hsmProxyApplication.handleCredentialUpload}"
						auto="true" label="Choose X509 certificate..."
						update="addCredentialForm" />
				</h:panelGrid>
				<p:commandButton value="Add"
					actionListener="#{hsmProxyApplication.addCredential}"
					oncomplete="handleAddApplicationCredentialRequest(xhr, status, args)"
					update="addCredentialForm,:messages,:mainForm" />
			</h:form>
		</p:dialog>

		<p:confirmDialog header="Remove Credential"
			message="Are you sure to remove the credential?" severity="alert"
			widgetVar="removeCredentialDialog">
			<h:form id="removeCredentialForm">
				<p:commandButton id="removeCredentialButton" value="Yes"
					actionListener="#{hsmProxyApplication.removeSelectedCredential}"
					oncomplete="removeCredentialDialog.hide();"
					update=":messages,:mainForm" />
				<p:commandButton value="No" onclick="removeCredentialDialog.hide();" />
			</h:form>
		</p:confirmDialog>

	</ui:define>
</ui:composition>